<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge,chrome=1">
    <meta name="referrer" content="never">
    <title>原型的动态性</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        ul {
            list-style: none;
        }
    </style>
</head>
<body>
    
    <script type="text/javascript">
        /* 
        先声明实例，然后再给原型添加属性，实例仍然可以使用添加的属性 -- 原型的动态性
        本质：实例原型属性 -> 函数的原型对象
        实例指向的原型对象未更改
        */

        function User(name){
            this.name = name;
        }

        var libai = new User("李白");
        // 原始User原型对象
        var originPrototype = User.prototype;
        // 重新赋值的原型对象
        var anotherPrototype = {
            constructor: User,
            age: 25,
            friends: [],
            sayName(){
                console.log(this.name);
            }
        }
        
        // User.prototype.age = 25;
        User.prototype = anotherPrototype;
        
        // sushi.__proto__ === anotherPrototype
        var sushi = new User("苏轼");

        console.log("李白是旧的", libai.__proto__ === originPrototype);
        console.log("苏轼是新的", sushi.__proto__ === anotherPrototype);
        
        // libai.__proto__ === originPrototype
        // console.log(libai.age); // 25;
        // libai.sayName();
    </script>
</body>
</html>